import {
	requestPayment
} from '@/utils/app'
export default {
	data() {
		return {
			addrInfo: {},
			couponText: '',
			couponInfo: {},
			allPrice: 0,
			goods: {},
			couponList: [],
			payType: '1', // 1微信  2积分
		};
	},
	async onLoad(options) {
		this.payType = options.payType
		this.goods = options.goods ? JSON.parse(decodeURIComponent(options.goods)) : {}
		await this.getAddrList()

		// 选择地址
		uni.$on('chooseAddr', res => {
			this.addrInfo = res.addr
		});

		// 选择优惠券
		uni.$on('chooseCoupon', res => {
			this.couponInfo = res.coupon
			var index = res.index
			this.goodsList[index].coupon_text =
				`满${this.couponInfo.detail.mini}减${this.couponInfo.detail.price}`;

			this.goodsList[index].coupon_price = this.couponInfo.detail.price
			this.goodsList[index].coupon_id = this.couponInfo.id

			this.$set(this.goodsList, index, this.goodsList[index]);
			this.getAllPrice()
		});
	},
	methods: {
		// 获取地址列表
		async getAddrList() {
			var res = await this.$get('server/index/addressList');
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			if (res.list.length == 0) {
				this.addrInfo = null
			} else {
				this.addrInfo = res.list[0]
			}
		},
		// 新增收货地址
		addAddr() {
			console.log('点击添加地址')
			this.$navTo('pages/mine/address-edit/address-edit', {
				isOrder: 1
			})
		},
		// 选择收货地址
		chooseAddr() {
			this.$navTo('pages/mine/address/address', {
				choose: 1
			})
		},
		// 计算总价
		getAllPrice() {
			this.allPrice = 0
			this.goodsList.forEach((item) => {
				this.allPrice += (Number(item.amount) - Number(item.coupon_price))
			})
		},
		// 下单
		async onPayOrder() {
			var res = await this.$post('server/index/buyshopgoods', {
				addressid: this.addrInfo.id,
				goodsid: this.goods.id,
				pay_state: this.payType,
				num: this.goods.num,
				specs: this.goods.specIdList
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('下单成功')

			requestPayment(res, this.payType)
				.then(() => {
					this.$toast('支付成功')
					setTimeout(() => {
						this.$redirectTo('pages/mine/order/order', {
							type: 0
						})
					}, 500)
				})
				.catch(err => this.$toast('订单未支付'))
				.finally(() => {
					this.$toast('取消支付')
				})
		},
		async showPay() {
			if (this.payType == 2) {
				// 积分下单
				var res = await this.$post('server/index/buyshopgoods', {
					goodsid: this.goods.id,
					pay_state: this.payType,
					num: this.goods.num,
					addressid: this.addrInfo.id,
					specs: this.goods.specIdList
				});
				if (res.errcode != 0) {
					this.$toast(res.errmsg);
					return;
				}
				this.$toast('兑换成功')
				
				setTimeout(() => {
					this.$redirectTo('pages/mine/order/order', {
						type: 1
					})
				}, 500)
			} else {
				this.$refs.pay.open();
			}

		},
		closePay() {
			this.$refs.pay.close();
		},
		// 选择优惠券
		onCoupon(item, index) {
			this.$navTo('pages/mine/coupon/coupon', {
				choose: 1,
				allPrice: this.allPrice,
				index: index
			})
		},
	}
}
